package com.hongdee.crm.question.service.Impl;

import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.question.dao.ITCrmQuestionDao;
import com.hongdee.crm.question.entity.TCrmSurveryQuestion;
import com.hongdee.crm.question.service.ITCrmQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Name: 问卷管理
 * @Author: Liu XiaoLong
 * @Date: 2017/10/13 10:32
 * @Remark:
 */
@Service
public class TCrmQuestionServiceImpl implements ITCrmQuestionService{
    @Autowired
    private ITCrmQuestionDao itCrmQuestionDao;
    private String tabName = "T_CRM_SURVERY_QUESTION";
    /**
     * @Name: 新增编辑
     * @Author: Liu XiaoLong
     * @Date: 2017/10/13 10:32
     * @Remark:
     */
    @Override
    public TCrmSurveryQuestion saveOrUpdate(String uid,TCrmSurveryQuestion tCrmSurveryQuestion) throws Exception {

        if(tCrmSurveryQuestion.getId() != null){ // 编辑
            String sql = "SELECT * FROM "+tabName+" WHERE quesiton_code = ?  AND id<> ? ";
            List params = new ArrayList();
            params.add(tCrmSurveryQuestion.getQuesitonCode());
            params.add(tCrmSurveryQuestion.getId());
            List list = itCrmQuestionDao.list(sql,params.toArray());

            if(list.size()>0){
                throw new Exception("问题编号已存在");
            }
            else {
                tCrmSurveryQuestion.setCompanyId(TokenUtil.getCompanyId(uid));
                tCrmSurveryQuestion.setStatus("0");
                itCrmQuestionDao.saveOrUpdate(tCrmSurveryQuestion);
            }
        }
        else { // 新增
            String sql = "SELECT * FROM "+tabName+" WHERE quesiton_code = ? ";
            List params = new ArrayList();
            params.add(tCrmSurveryQuestion.getQuesitonCode());
            List list = itCrmQuestionDao.list(sql,params.toArray());

            if(list.size()>0){
                throw new Exception("问题编号已存在");
            }
            else {
                tCrmSurveryQuestion.setCompanyId(TokenUtil.getCompanyId(uid));
                tCrmSurveryQuestion.setStatus("0");
                itCrmQuestionDao.saveOrUpdate(tCrmSurveryQuestion);
            }
        }



        return tCrmSurveryQuestion;
    }

    /**
     * @Name: 分页查询
     * @Author: Liu XiaoLong
     * @Date: 2017/10/13 10:33
     * @Remark:
     */
    @Override
    public Map page(String start, String limit, String questionName) throws Exception {
        //String questionName = (String) data.get("questionName");
        String sql = "SELECT * FROM "+tabName+" WHERE 1=1 ";
        List params = new ArrayList();
        if(!questionName.isEmpty()){
            sql += " AND quesiton_name LIKE ? ";
            params.add("%"+questionName+"%");
        }
        Map map = itCrmQuestionDao.page(sql,params.toArray(),start,limit);

        return map;
    }

    /**
     * @Name: 启用
     * @Author: Liu XiaoLong
     * @Date: 2017/10/13 10:33
     * @Remark:
     */
    @Override
    public Map start(String id) throws Exception {
        String hql = "update TCrmSurveryQuestion set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", "1");
        itCrmQuestionDao.update(hql,params);
        return null;
    }

    /**
     * @Name: 停用
     * @Author: Liu XiaoLong
     * @Date: 2017/10/13 10:33
     * @Remark:
     */
    @Override
    public Map stop(String id) throws Exception {
        String hql = "update TCrmSurveryQuestion set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", "0");
        itCrmQuestionDao.update(hql,params);
        return null;
    }

    /**
     * @Name: 获取问题树
     * @Author: Liu XiaoLong
     * @Date: 2017/10/13 16:28
     * @Remark:
     */
    @Override
    public Map questionTree(String uid) throws Exception {

        String companyId = TokenUtil.getCompanyId(uid);
        List<Map<String, Object>> parentsList = this.getPOrCList(companyId,"");
        List<Map<String, Object>> childsList = this.getPOrCList(companyId,"child");
        for(int i=0;i<parentsList.size();i++){
            List<Map<String, Object>> tempList = new ArrayList<>();
            for(int j=0;j<childsList.size();j++){
                if(childsList.get(j).get("groupCode").equals(parentsList.get(i).get("groupCode"))){
                    childsList.get(j).put("tName",childsList.get(j).get("itemName"));
                    childsList.get(j).put("tType","1");
                    childsList.get(j).put("groupId",parentsList.get(i).get("id"));
                    tempList.add(childsList.get(j));
                }
            }
            parentsList.get(i).put("tName",parentsList.get(i).get("groupName"));
            parentsList.get(i).put("tType","0");
            parentsList.get(i).put("child",tempList);
        }
        Map map = new HashMap();
        map.put("dataList",parentsList);
        return map;
    }

    /**
     * @Name: 获取问卷预览
     * @Author: Liu XiaoLong
     * @Date: 2017/10/17 10:59
     * @Remark:
     */
    @Override
    public Map getQuestionPage(String uid, String questionId) throws Exception {
        String companyId = TokenUtil.getCompanyId(uid);
        // 分类查询
        String groupSql = "SELECT a.group_id,a.quesiton_id,a.company_id,b.group_code,b.group_name " +
                "FROM t_crm_survery_question_group a LEFT JOIN  t_crm_survery_group b ON a.group_id = b.id "+
                "WHERE a.quesiton_id = ? AND a.company_id = ? ";
        List groupParams = new ArrayList();
        groupParams.add(questionId);
        groupParams.add(companyId);
        List groupResp = itCrmQuestionDao.list(groupSql,groupParams.toArray());
        for(int i = 0;i<groupResp.size();i++){
            Map groupMap = (Map) groupResp.get(i);
            // 问题项查询
            String itemSql = "SELECT a.id,a.item_id,a.group_id,a.quesiton_id,a.company_id,b.item_code,b.item_name,b.group_code,b.item_type " +
                    "FROM t_crm_survery_question_group_item a LEFT JOIN  t_crm_survery_item b ON a.item_id = b.id "+
                    "WHERE a.quesiton_id = ? AND a.company_id = ? AND a.group_id  = ? ";
            List itemParams = new ArrayList();
            itemParams.add(questionId);
            itemParams.add(companyId);
            itemParams.add(groupMap.get("groupId"));
            List itemResp = itCrmQuestionDao.list(itemSql,itemParams.toArray());
            for(int j = 0;j<itemResp.size();j++){
                Map itemMap = (Map) itemResp.get(j);
                // 评分项查询
                String areaSql = "SELECT a.id,a.item_id,a.group_id,a.quesiton_id,a.company_id,b.area_code,b.area_name,b.group_code " +
                        "FROM t_crm_survery_question_group_item_area a LEFT JOIN  t_crm_survery_item_area b ON a.area_id = b.id " +
                        "WHERE a.quesiton_id = ? AND a.company_id =? AND a.item_id  = ? ";
                List areaParams = new ArrayList();
                areaParams.add(questionId);
                areaParams.add(companyId);
                areaParams.add(itemMap.get("itemId"));
                List areaResp = itCrmQuestionDao.list(areaSql,areaParams.toArray());
                ((Map) itemResp.get(j)).put("area",areaResp);
            }
            ((Map) groupResp.get(i)).put("item",itemResp);
        }
        Map questionPageMap = new HashMap();
        questionPageMap.put("dataList",groupResp);

        return questionPageMap;
    }



    /**
     * @Name: 问卷删除
     * @Author: Liu XiaoLong
     * @Date: 2017/10/17 14:41
     * @Remark:
     */
    @Override
    public Map questionDelete(String uid, String questionId) throws Exception {
        String sql = "DELETE FROM t_crm_survery_question WHERE quesiton_code= '"+questionId+"' ";
        itCrmQuestionDao.executeSQL(sql);

        String[] tableNames = {"t_crm_survery_question_group"
                ,"t_crm_survery_question_group_item"
                ,"t_crm_survery_question_group_item_area"};
        for(int i=0;i<3;i++){
            String sqlIn = "DELETE FROM "+tableNames[i]+" WHERE quesiton_id= '"+questionId+"' ";
            itCrmQuestionDao.executeSQL(sqlIn);
        }
        Map map  = new HashMap();
        return map;
    }

    /**
     * @Name: 详情查询
     * @Author: Liu XiaoLong
     * @Date: 2017/10/17 17:35
     * @Remark:
     */
    @Override
    public Map info(String uid, String id) throws Exception {

        String sql  = "SELECT * FROM "+tabName+" WHERE company_id = ? AND id = ? ";
        List params =  new ArrayList();
        params.add(TokenUtil.getCompanyId(uid));
        params.add(id);
        Map map = new HashMap();
        map.put("dataList",itCrmQuestionDao.info(sql,params.toArray()));
        return map;
    }

    /**获取问题树父节点或子节点**/
    public List getPOrCList(String companyId,String parentsOrChild) throws Exception {
        List<Map<String, Object>> parentsList;
        List params = new ArrayList();
        String sql = "";
        if(parentsOrChild.equals("child")){
            sql = "SELECT * FROM t_crm_survery_item WHERE company_id = ? ";
        }
        else {
            sql = "SELECT * FROM t_crm_survery_group WHERE company_id = ? ";
        }
        params.add(companyId);
        parentsList  = itCrmQuestionDao.list(sql,params.toArray());
        return parentsList;
    }


}
